package com.dsprun.dspai.config;

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * AI服务配置类，用于创建和配置各种AI模型Bean
 */
@Configuration
public class AiConfig {

    @Value("${app.ai.deepseek.api-key}")
    private String deepseekApiKey;  // DeepSeek API密钥

    @Value("${app.ai.deepseek.model}")
    private String deepseekModel;   // DeepSeek模型名称

    @Value("${app.ai.deepseek.temperature}")
    private Double deepseekTemperature; // 生成温度参数

    @Value("${app.ai.deepseek.timeout}")
    private Integer deepseekTimeout;    // API调用超时时间

    @Value("${app.ai.embedding.model}")
    private String embeddingModel;      // 嵌入模型名称

    /**
     * 创建DeepSeek聊天模型Bean
     * @return 配置好的ChatLanguageModel实例
     */
    @Bean
    public ChatLanguageModel deepseekChatModel() {
        return OpenAiChatModel.builder()
                .apiKey(deepseekApiKey)
                .modelName(deepseekModel)
                .temperature(deepseekTemperature)
                .timeout(Duration.ofSeconds(deepseekTimeout))
                .baseUrl("https://api.deepseek.com/v1") // DeepSeek API基础URL
                .build();
    }

    /**
     * 创建DeepSeek流式聊天模型Bean
     * @return 配置好的StreamingChatLanguageModel实例
     */
    @Bean
    public StreamingChatLanguageModel deepseekStreamingChatModel() {
        return OpenAiStreamingChatModel.builder()
                .apiKey(deepseekApiKey)
                .modelName(deepseekModel)
                .temperature(deepseekTemperature)
                .timeout(Duration.ofSeconds(deepseekTimeout))
                .baseUrl("https://api.deepseek.com/v1")
                .build();
    }

    /**
     * 创建嵌入模型Bean
     * @return 配置好的EmbeddingModel实例
     */
    @Bean
    public EmbeddingModel embeddingModel() {
        return OpenAiEmbeddingModel.builder()
                .apiKey(deepseekApiKey)
                .modelName(embeddingModel)
                .baseUrl("https://api.deepseek.com/v1")
                .build();
    }
}